<template>
  <div>
    <van-search
      clearable
      background="transparent"
      v-model="params.whereaboutsEnterpriseName"
      class="b-b-2"
      placeholder="搜索企业名称"
      @search="onSearch"
    />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <van-cell v-for="(item, key) in list" :key="key" class="approvalItem">
          <div class="content" @click="detailsGo(item.id)">
            <div class="flex-between font-14 line-height-3">
              <div class="col-333333 font-bold-500 font-15 m-r-10">{{ item.salesBatchNo }}</div>
              <div class="col-999999 line-text-out text-right flex align-items">
                <div class="m-l-5 nj lo" v-if="item.salesStatus == 0">未生效</div>
                <div v-if="item.salesStatus == 1" class="m-l-5 nj la">正常</div>
                <div v-if="item.salesStatus == 2" class="m-l-5 nj lk">已作废</div>
              </div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 m-r-10 line-text-out">销售日期：{{ item.salesDate }}</div>
              <div class="col-666666 m-r-10 line-text-out" v-if="milkStationFlag == 0">
                生鲜乳来源（家）:{{ item.sourceNumber }}
              </div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 m-r-10">生鲜乳总数量（kg）：{{ item.freshMilkTotal }}</div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 m-r-10 line-text-out" v-if="item.subjectEnterpriseInfo">
                销售去向：{{ item.subjectEnterpriseInfo.enterpriseName }}
              </div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 m-r-10 line-text-out">创建人：{{ item.createdByName }}</div>
              <div class="col-666666 m-r-10 line-text-out">创建时间：{{ item.createTime }}</div>
            </div>
          </div>
          <div class="border"></div>
          <div class="flex-end m-t-10">
            <van-button class="btn m-r-15" v-if="item.salesStatus == 0" @click="qrCodeHandle(item.id)"
              >生成二维码</van-button
            >
            <van-button class="nj-btn m-r-10" v-if="item.salesStatus == 1" @click="invalidGo(item.id)">作废</van-button>
            <van-button
              class="btn m-r-15"
              native-type="button"
              v-if="item.salesStatus == 2 || item.salesStatus == 1"
              @click="CodeViewGo(item.id)"
              >查看二维码</van-button
            >
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import { getValue, setValue } from '@/utils/auth'
import { Toast, Cell, CellGroup, Popup, Picker, PullRefresh, List, Button, Dialog, Search } from 'vant'
import { getHandle, postHandle } from '@/api'
export default {
  components: {
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Popup.name]: Popup,
    [Picker.name]: Picker,
    [PullRefresh.name]: PullRefresh,
    [Toast.name]: Toast,
    [List.name]: List,
    [Button.name]: Button,
    [Toast.name]: Toast,
    [Dialog.name]: Toast,
    [Search.name]: Search
  },
  data() {
    return {
      refreshing: false,
      list: [],
      loading: false,
      finished: false,
      milkStationFlag: getValue('milkStationFlag'),
      params: {
        page: 1,
        perPage: 10,
        subjectInfoId: getValue('subjectInfoId'),
        tenantCode: getValue('tenantCode')
      }
    }
  },
  mounted() {
    this.$bridge.register('refresh', data => {
      this.refreshing = true
      this.params.page = 1
      this.onLoad()
    })
  },
  methods: {
    // 卡片列表
    onRefresh() {
      this.params.page = 1
      this.finished = false
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onSearch() {
      this.params.page = 1
      this.list = []
      this.finished = false
      this.loading = true
      this.onLoad()
    },
    onLoad() {
      getHandle(`/agri/api/yzbzh/saleInfo/queryListPage`, this.params).then(res => {
        if (this.refreshing) {
          this.list = []
          this.refreshing = false
        }
        this.list = this.list.concat(res.data.rows)
        this.loading = false
        if (this.list.length >= res.data.count) {
          this.finished = true
        } else {
          this.params.page = this.params.page + 1
        }
      })
    },
    qrCodeHandle(val) {
      Dialog.confirm({
        title: '生成二维码确认',
        confirmButtonColor: 'black',
        message: '二维码生成后不能修改，确定要生成吗？'
      })
        .then(() => {
          postHandle(`/agri/api/yzbzh/saleInfo/generateQRCode/${val}`).then(res => {
            Toast.success('生成成功')
            this.list = []
            this.onLoad()
          })
        })
        .catch(() => {})
    },
    CodeViewGo(id) {
      setValue('milKId', id)
      this.$bridge.call('qrCodeView')
    },
    invalidGo(val) {
      setValue('invalidId', val)
      this.$bridge.call('invalidRecord')
    },
    detailsGo(val) {
      setValue('milKId', val)
      this.$bridge.call('milkDetails')
    }
  }
}
</script>
<style lang="scss" scoped>
.topCeiling {
  position: fixed;
  top: 0;
  z-index: 1;
}
.topDate {
  width: 80%;
  margin: auto;
  display: flex;
  justify-content: space-between;
  > p {
    font-size: 15px;
    &:first-child {
      width: 40%;
    }
    &:last-child {
      width: 40%;
    }
  }
}
.van-field__control {
  border: 0 !important;
}
.content {
  //   border-bottom: 1px solid #eee;
  padding: 10px 15px;
}
.nj {
  height: 20px;
  width: 50px;
  border-radius: 10px;
  font-size: 12px;
  text-align: center;
  line-height: 20px;
}
.lo {
  background: rgba(255, 191, 31, 0.15);
  color: #ffbf1f;
}
.la {
  background: rgba(55, 181, 181, 0.15);
  color: #37b5b5;
}
.lk {
  background: rgba(153, 153, 153, 0.15);
  color: #999999;
}
.border {
  height: 1px;
  background: #eeeeee;
}
.btn {
  height: 26px;
  width: 90px;
  background: #37b5b5;
  color: #ffffff;
  white-space: nowrap;
  font-size: 13px;
  border-radius: 4px;
}
.nj-btn {
  height: 26px;
  width: 50px;
  border: 1px solid #37b5b5;
  background: #ffffff;
  color: #37b5b5;
  white-space: nowrap;
  font-size: 13px;
  border-radius: 4px;
}
.b-b-2 {
  padding-bottom: 0;
}
::v-deep .van-search {
  width: 98%;
  margin: auto;
}
::v-deep .van-search__content {
  background-color: #fff;
  border-radius: 34px;
}
</style>
